package com.scenery.server.services;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import com.scenery.server.util.Log;
import com.scenery.server.vo.User;

public class UserServices extends Manager{
	static String tag=UserServices.class.getName();
	/***
	 * 用户注册
	 * 
	 * */
	public static int registerOpenfire(String username,String password)
	{
		ConnectionConfiguration config = new ConnectionConfiguration("192.168.7.51",5222);
		org.jivesoftware.smack.Connection connection = new XMPPConnection(config);
		try {
			connection.connect();
			AccountManager amgr = connection.getAccountManager();
			amgr.createAccount(username, password);
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		return 0;


	}
	/**
	 * 登陆
	 * 2用户名不在
	 * 1成功
	 * 3密码错误
	 * 4程序异常
	 * **/
	public int login(String username,String password) {
		Connection conn = null;
		Statement statement = null;
		ResultSet rs = null;
		String sql = "select password from lv_user where userId='"+username.trim()+"'";
		System.out.println(sql);
		int ret = 0;
		String pwd="";
		conn = getConnection();
		if (conn != null) {
			try {
				statement = conn.createStatement();
				rs = statement.executeQuery(sql);
				while (rs.next()) 
				{
					pwd=rs.getString(1);
					
				}
				System.out.println("".equals(pwd));
				if("".equals(pwd))
				{
					ret=2;
				}
				else 
				{
					if(password.equals(pwd))
					{
						ret=1;
					}
					else 
					{
						ret=3;
					}
					
				}
				rs.close();
				statement.close();
				conn.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=4;
			}

		}
		return ret;
	}
	/**
	 * 注册
	 * **/
	public static  int insert(String username,String password)
	{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		String sql = "insert into  lv_user ( `userId`, `password`) " +
				"VALUES  (?,?) ";
		int ret=1;
		

		
		conn = getConnection();
		if (conn != null) {
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, username);
				ps.setString(2,password);
						
				
				
				ps.executeUpdate();
				ps.close();
				conn.close();
				ret=0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=3;
			}

		}
		return ret;
	}
	public int isFirst(String username) {
		Connection conn = null;
		Statement statement = null;
		ResultSet rs = null;
		String sql = "select userId from lv_user where userId='"+username.trim()+"'";
		System.out.println(sql);
		int ret = 0;
		String pwd="";
		conn = getConnection();
		if (conn != null) {
			try {
				statement = conn.createStatement();
				rs = statement.executeQuery(sql);
				while (rs.next()) 
				{
					pwd=rs.getString(1);
					ret=1;
				}
				
				rs.close();
				statement.close();
				conn.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=4;
			}

		}
		return ret;
	}
	/**
	 * 记录用户坐标
	 * **/
	public static  int addUserLocation(User user)
	{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		String sql = "insert into  lv_user ( userId, lon,lat) " +
				"VALUES  (?,?,?) ";
		int ret=1;
		

		
		conn = getConnection();
		if (conn != null) {
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, user.getUserId());
				ps.setString(2,user.getLon());
				ps.setString(3,user.getLat());		
				
				
				ps.executeUpdate();
				ps.close();
				conn.close();
				ret=0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=3;
			}

		}
		return ret;
	}
	/**
	 *更新用户坐标
	 * **/
	public static  int updateUserLocation(User user)
	{
		Log.d(tag, "更新用户坐标");
		Connection conn = null;
		PreparedStatement ps = null;
		
		String sql = "update  lv_user set lon=? ,lat=? where userId=? " 
				;
		int ret=1;
		

		
		conn = getConnection();
		if (conn != null) {
			try {
				ps = conn.prepareStatement(sql);
				
				ps.setString(1,user.getLon());
				ps.setString(2,user.getLat());		
				ps.setString(3, user.getUserId());
				
				ps.executeUpdate();
				ps.close();
				conn.close();
				ret=0;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret=3;
			}

		}
		return ret;
	}
	/***
	 * 查看好友坐标
	 * **/
	public static List<User> getOther(String userid)
	{
		Log.d(tag, "查看好友坐标");
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select  `userId`, `lon`,  `lat` from  lv_user where userId !='"+userid+"'";
		Log.d(tag, "sql:"+sql);
		
		List<User> list=new ArrayList<User>();
		
		conn = getConnection();
		if (conn != null) {
			try {
				ps = conn.prepareStatement(sql);
				rs=ps.executeQuery();
				while(rs.next())
				{
					User bean =new User();
					bean.setUserId(rs.getString(1));
				
					
					bean.setLon(rs.getString(2));
					bean.setLat(rs.getString(3));
					
					list.add(bean);
				}
				rs.close();
				ps.close();
				conn.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}

		}
		return list;
	}
	/*** 用户注册OpenFire */
	public static String loginOpenFire(String username, String password) {
		
		String ret = "-1";
		ConnectionConfiguration config = new ConnectionConfiguration("192.168.7.51", 5222);
		org.jivesoftware.smack.Connection connection = new XMPPConnection(config);
		try {
			connection.connect();

			connection.login(username, password);

			ret = "0";

		} catch (XMPPException e) {
			// TODO Auto-generated catch block
		
			e.printStackTrace();
			ret = "4";
		}

		return ret;
	}
	public static void main(String args[])
	{
		//registerOpenfire("u2","123");
		//p(loginOpenFire("u2","123"));
		//insert("zhao1","zhao");
		//insert("zhao","zhao");
		//p(getOther("zhao").size());
		String s="113.3736572265625";
		System.out.println(new Double(s));
	}
	
	
}
